#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

//重定向的底层接口
int test1()
{
    int fd = open("log.txt",O_APPEND|O_RDWR|O_CREAT,0666);
    dup2(fd,1);
    close(fd);
    // const char* str1 = "hello";
    char str1[]= "hello\n";
    // printf("%d\n",fd);
    int len = write(1,str1,sizeof(str1));
    return 0;
}

//重定向操作符的使用
void test2()
{
    int fd = open("error.log",O_APPEND|O_RDWR);
    fprintf(stdout,"%s","stdout print\n");
    fprintf(stdout,"%s","stdout print\n");
    fprintf(stdout,"%s","stdout print\n");

    char str1[] = "stderr print\n";
    write(fd,str1,sizeof(str1));
    fprintf(stderr,"%s","stderr print\n");
    fprintf(stderr,"%s","stderr print\n");
    fprintf(stderr,"%s","stderr print\n");
    close(fd);
}


int main()
{
    test1();
    return 0;
}